package pms.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.protocol.DataSource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import pms.om.Image;
import pms.om.Raum;

import com.mysql.jdbc.Blob;
import de.fhd.jface.*;

/** 
 * Controller to generate the Home Page basics to be rendered by a view. 
 * It extends the convenience class AbstractController that encapsulates most 
 * of the drudgery involved in handling HTTP requests. 
 * @author josef@dr-schneeberger.de
 */
public class SaveRaum extends AlleRaeume {
	protected ModelAndView handleRequestInternal(
			HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		httpServletRequest.setCharacterEncoding("UTF-8");
		int id = Integer.parseInt(httpServletRequest.getParameter("id"));
		String name = httpServletRequest.getParameter("name");
	    

	    
	     
	     int testNr = getRaumDao().getMaxNrOfId();
		    System.out.println("testNr:"+testNr);
			
		    
		    List Image = getRaumDao().findAll();
		    System.out.println("testImageSize:"+Image.size());
	 	    
		  //String test = getServletContext().getRealPath("/");
	      //String test1 = getServletContext().getContextPath();
	      //System.out.println("testDir :"+test1);
		  
  //Get all images from database / at the moment limited to 4 because out of heap size memory 
/*  for (int idDs = 1; idDs <= 4 ; idDs++) {
		    
		    Raum dbImage = getRaumDao().findById(idDs);
		    byte[] bImage1 = dbImage.getPicture1();
		    byte[] bImage2 = dbImage.getPicture2();
		    byte[] bImage3 = dbImage.getPicture3();
		    byte[] bImage4 = dbImage.getPicture4();
		    byte[] bImage5 = dbImage.getPicture5();
		    byte[] bImage6 = dbImage.getPicture6();
		    byte[] bImage7 = dbImage.getPicture7();
		    byte[] bImage8 = dbImage.getPicture8();
		    byte[] bImage9 = dbImage.getPicture9();
		    byte[] bImage10 = dbImage.getPicture10();
		    byte[] bImage11 = dbImage.getPicture11();
		    byte[] bImage12 = dbImage.getPicture12();
		    byte[] bImage13 = dbImage.getPicture13();
		    byte[] bImage14 = dbImage.getPicture14();
		    byte[] bImage15 = dbImage.getPicture15();
		    byte[] bImage16 = dbImage.getPicture16();
		    
        try{
	      FileOutputStream fos1 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 0 + ".jpg"); fos1.write(bImage1); fos1.close();
	      FileOutputStream fos2 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 1 + ".jpg"); fos2.write(bImage2); fos2.close();
	      FileOutputStream fos3 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 2 + ".jpg"); fos3.write(bImage3); fos3.close();
	      FileOutputStream fos4 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 3 + ".jpg"); fos4.write(bImage4); fos4.close();
	      FileOutputStream fos5 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 4 + ".jpg"); fos5.write(bImage5); fos5.close();
	      FileOutputStream fos6 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 5 + ".jpg"); fos6.write(bImage6); fos6.close();
	      FileOutputStream fos7 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 6 + ".jpg"); fos7.write(bImage7); fos7.close();
	      FileOutputStream fos8 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 7 + ".jpg"); fos8.write(bImage8); fos8.close();
	      FileOutputStream fos9 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 8 + ".jpg"); fos9.write(bImage9); fos9.close();
	      FileOutputStream fos10 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 9 + ".jpg"); fos10.write(bImage10); fos10.close();
	      FileOutputStream fos11 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 10 + ".jpg"); fos11.write(bImage11); fos11.close();
	      FileOutputStream fos12 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 11 + ".jpg"); fos12.write(bImage12); fos12.close();
	      FileOutputStream fos13 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 12 + ".jpg"); fos13.write(bImage13); fos13.close();
	      FileOutputStream fos14 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 13 + ".jpg"); fos14.write(bImage14); fos14.close();
	      FileOutputStream fos15 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 14 + ".jpg"); fos15.write(bImage15); fos15.close();
	      FileOutputStream fos16 = new FileOutputStream(ImagePath + "AllDbImage/" + idDs + "_" + 15 + ".jpg"); fos16.write(bImage16); fos16.close();
	      
       
        }catch(Exception e){
            e.printStackTrace();
        }
  }*/
		int personenAnzahl = Integer.parseInt(httpServletRequest.getParameter("personenAnzahl"));
		
		String klasse = httpServletRequest.getParameter("klasse");
		if (id == 0) {
			getRaumDao().create(name, personenAnzahl, klasse);
		} else {
			getRaumDao().modify(id, name, personenAnzahl, klasse);
		}
		return super.handleRequestInternal(httpServletRequest, httpServletResponse);
	}
}
	



